<!DOCTYPE html>
<html>

<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1, minimum-scale=1.0, maximum-scale=1.0, user-scalable=no">
    <meta name="description" content="">
    <meta name="author" content="">
    <title>doc view</title>
    <link href="css/bootstrap.min.css" rel="stylesheet">
    <link href="css/style.css" rel="stylesheet">
</head>

<body>
    <div id="webConsole"><textarea readonly="readonly"></textarea></div>
    <div id="viewMain" style="bottom:5px">
        <div id="viewBody" class="container" style="background-color:#fff;position:absolute;top:0;bottom:60px;width:100%">
            <iframe id="fr0" class="fullscreenframe" src="about:blank"></iframe>
        </div>
        <div id="loadPromt" class="view-loading">
            <img id="view-loading-img" class="view-loading-img" src="img/loading.gif">
            <div id="view-loading-retry" class="view-loading-retry">
                <div class="loading-retry-text"></div>
                <div class="loading-retry-button" data-i18n="retry"></div>
            </div>
        </div>
        <div class="footer text-muted" style="position: absolute;bottom: 0" data-i18n="notice"></div>
    </div>

    <script src="js/jquery-3.3.1.min.js"></script>
    <script src="js/jquery.i18n.js"></script>
    <script src="js/jquery.i18n.messagestore.js"></script>
    <script src="js/main.js"></script>
    <script>
        (function ($) {
          var logger = $.logger

          var authAllowed = false
          var pageDataReady = false
          var baseUrl

          var isInitPages = false

          $(function () {
            $('#viewMain').i18n()

            initJSAPI()

            $.SDK.jsComplete()
          })

          function initJSAPI () {
            window.view = function (docViewUrl, sysCode, viewInfo, localCache) {
              logger.debug('SDK -> view(' + docViewUrl + ',' + sysCode + ',' + viewInfo + ',' + localCache + ')')
              baseUrl = docViewUrl
              $.initDocView(docViewUrl, sysCode, viewInfo)
              appendImg()
              return '1'
            }

            window.authResult = function (code) {
              $.logger.debug('SDK -> authResult(' + code + ')')
              switch (code) {
                case '100':
                  authSuccess()
                  break
                case '101':
                  showRefuse(code)
                  break
                case '102':
                case '103':
                  showAuthRetry(code)
                  break
                default:
                  break
              }
              return '1'
            }

            window.cacheImgCallback = function (page, chunk, result, cacheUrl) {
              logger.debug('SDK -> cacheImgCallback(' + page + ',' + chunk + ',' + result + ',' + cacheUrl + ')')
              return '1'
            }
          }

          var authSuccess = function () {
            $.logger.debug('已授权')
            authAllowed = true
            showView()
            authSuccess = showRefuse = showAuthRetry = $.noop
          }

          function showView () {
            if (!authAllowed || !pageDataReady) return
            $('#viewBody').show()
            $('.footer').show()
            $('#loadPromt').hide()
          }

          var showRefuse = function (code) {
            $.logger.debug('未授权')
            $('#viewBody').empty()
            $.throwError(code, null)
            authSuccess = showRefuse = showAuthRetry = $.noop
          }

          var showAuthRetry = function (code) {
            $.logger.debug('鉴权失败' + code)
            $.throwError(code, $.SDK.authRetry)
          }

          function appendImg () {
            var page = 1
            logger.debug('开始远程加载：' + page)
            $.remoteLoad({
              starPage: page,
              startPage: page,
              endPage: page
            }, function (json) {
              logger.debug('远程调用成功响应[' + page + ']：' + JSON.stringify(json))
              if (!json.error && !!json.data && json.data.Urls.length > 0) {
                if (!isInitPages) {
                  var url = json.data.Urls[0].Url + '?idBg=about:blank'
                  logger.debug('加载URL：' + url)
                  $('#fr0').attr('src', baseUrl + url)
                  isInitPages = true
                }
                pageDataReady = true
                showView()
              } else {
                var errorCode
                if (json.code === 200) {
                  // 由转换服务返回的错误
                  switch (json.error.code) {
                    case 'DOWNLOAD_FAILED':
                      errorCode = '510'
                      break
                    case 'FILE_SIZE_OUT_OF_LIMIT':
                      errorCode = '511'
                      break
                    default:
                      errorCode = '502'
                      break
                  }
                } else {
                  // api返回的错误
                  errorCode = json.code.toString()
                }
                $.throwError(errorCode, function () {
                  logger.debug('重试appendImg：' + page)
                  appendImg()
                })
              }
            }, function (XMLHttpRequest, textStatus, errorThrown) {
              logger.error('请求失败[' + page + '-' + page + ']：textStatus=' + textStatus + ',errorThrown=' + errorThrown)
              var errorCode = '400'
              if (textStatus === 'timeout') {
                errorCode = '504'
              }
              $.throwError(errorCode, function () {
                logger.debug('重试appendImg：' + page)
                appendImg()
              })
            })
          }
        })(window.jQuery)
    </script>
</body>

</html>